﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Controls;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Windows;
using System.Collections.ObjectModel;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    public class PersonnelViewModel : DataListViewModel
    {
        private UserControl gWindow;
        private ParentWindowViewModel gParentWindowViewModel;
        private PersonnelModel gPersonnelModel;
        

        public PersonnelViewModel(UserControl pWindow, ParentWindowViewModel pParentWindowViewModel)
        {
            gWindow = pWindow;
            gParentWindowViewModel = pParentWindowViewModel;
            gPersonnelModel = new PersonnelModel();

            UpdateList();
        }

        public void UpdateList()
        {
            DataList.Clear();

            List<WPersonnel> list = WPersonnel.GetAll();
            for (int i = 0; i < list.Count; i++)
            {
                LPersonnel item = new LPersonnel();
                item.IsChoose = false;
                item.PersonnelUID = list[i].PersonnelUID;
                item.ID = list[i].ID;
                item.Password = list[i].Password;
                item.PersonnelGroupUID = list[i].PersonnelGroupUID;
                item.PesonnelGroupName = WPersonnelGroup.GetPersonnelGroupName(list[i].PersonnelGroupUID);
                item.Name = list[i].Name;
                item.Head = list[i].Head;
                item.Mobile = list[i].Mobile;
                item.WebChat = list[i].WebChat;
                item.CreateTime = list[i].CreateTime;
                DataList.Add(item);
            }
        }

        public ObservableCollection<LPersonnel> DataList
        {
            get
            {
                return gPersonnelModel.DataList;
            }

            set
            {
                DataList = value;
                RaisePropertyChanged("DataList");
            }
        }

        public ObservableCollection<LPersonnel> DataListEdit
        {
            get
            {
                return gPersonnelModel.DataListEdit;
            }

            set
            {
                DataListEdit = value;
                RaisePropertyChanged("DataListEdit");
            }
        }

        public Visibility DataListEditVisibility
        {
            get
            {
                return gPersonnelModel.dataListEditVisibility;
            }
            set
            {
                gPersonnelModel.dataListEditVisibility = value;
                RaisePropertyChanged("DataListEditVisibility");
            }
        }

        protected override void DLCloseCommand(object obj)
        {
            gParentWindowViewModel.RemoveUserControl(gWindow.Uid);
        }

        protected override void DLSaveCommand(object obj)
        {
            List<WPersonnel> list = new List<WPersonnel>();
            foreach (LPersonnel personnel in DataListEdit)
            {
                if (personnel.IsChoose)
                {
                    list.Add(new WPersonnel(personnel));
                }
            }
            HttpResponse response = WPersonnel.Update(list);

            UpdateList();

            DataListEditVisibility = Visibility.Hidden;
            DLBSaveVisibility = Visibility.Hidden;
            EditMode = false;
        }

        protected override void DLQuitCommand(object obj)
        {
            gParentWindowViewModel.RemoveUserControl(gWindow.Uid);
        }

        protected override void DLGoCommand(object obj)
        {

        }

        protected override void DLPageUpCommand(object obj)
        {

        }

        protected override void DLPageDownCommand(object obj)
        {

        }

        protected override void DLCheckedCommand(object obj)
        {
            if (DLTIsChecked)
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = true;
                }
            }
            else
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = false;
                }
            }
        }

        protected override void DLAddCommand(object obj)
        {
            PersonnelInsert window = new PersonnelInsert();
            PersonnelInsertViewModel viewModel = new PersonnelInsertViewModel(window, this);
            window.DataContext = viewModel;
            window.ShowDialog();
        }

        protected override void DLEditCommand(object obj)
        {
            if(!EditMode)
            {
                DataListEdit.Clear();

                List<WPersonnelGroup> list = WPersonnelGroup.GetAll();
                foreach (LPersonnel personnel in DataList)
                {
                    if(personnel.IsChoose)
                    {
                        for(int i = 0; i < list.Count; i++)
                        {
                            personnel.PersonnelGroup.Add(list[i].PersonnelGroupName.Trim());
                        }
                        personnel.PersonnelGroupSelected = personnel.PesonnelGroupName;

                        DataListEdit.Add(personnel);
                    }
                }

                if (DataListEdit.Count>0)
                {
                    DataListEditVisibility = Visibility.Visible;
                    DLBSaveVisibility = Visibility.Visible;
                    EditMode = true;
                }
            }
        }

        protected override void DLDeleteCommand(object obj)
        {
            List<WPersonnel> list = new List<WPersonnel>();
            foreach (LPersonnel personnel in DataList)
            {
                if (personnel.IsChoose)
                {
                    list.Add(new WPersonnel(personnel));
                }
            }
            HttpResponse response = WPersonnel.Delete(list);

            UpdateList();
        }
    }
}
